<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="secondid" placeholder="二手商品编号" style="width: 200px;" />
      <el-button v-waves class="filter-item" type="warning" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >

      <el-table-column label="商品编号" prop="eid" sortable="custom" align="center" width="80" />
      <el-table-column label="二手商品上架日期" prop="ltime" width="150px" align="center" />
      <el-table-column label="二手商品名称" prop="rname" min-width="150px" />
      <el-table-column label="上架商品人员" prop="uname" width="110px" align="center" />
      <el-table-column label="库存" align="center" width="95" />

      <el-table-column label="状态" prop="status" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="440" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-if="row.status!='已通过'" size="mini" type="success" @click="handleModifyStatus(row,'已通过')">
            通过
          </el-button>

          <el-button v-if="row.status!='未通过'" size="mini" type="warning" @click="handleModifyStatus(row,'未通过')">
            不通过
          </el-button>

          <el-button size="mini" type="danger" @click="handleModifyStatus(row,'未审批')">
            放弃审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import reqwest from 'reqwest'

export default {
  name: 'ComplexTable',
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        '已审批': 'success',
        '未通过': 'warning',
        '未审批': 'info',
        '已删除': 'danger',
        '1': 'info',
        '2': 'success',
        '3': 'warning'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      statusOptions: ['已审批', '未通过', '未审批', '已删除'],
      downloadLoading: false,
      secondid: ''
    }
  },
  created() {
    this.getList(this.handleSuccess)
  },
  methods: {
    handleSuccess(res) {
      console.log(res)
      if (Array.isArray(res)) {
        this.list = res
      } else if (res instanceof Object) {
        this.list = [res]
      }
      this.listLoading = false
    },
    getList(callback) {
      this.listLoading = true
      reqwest({
        url: 'http://123.57.243.139:8080/examine/queryByType?type=3',
        crossOrigin: true,
        method: 'get',
        success: function(result) {
          callback(result)
        }
      })
    },
    getListByID(callback) {
      var id = this.secondid
      console.log('id = ' + id)
      this.listLoading = true
      reqwest({
        url: 'http://123.57.243.139:8080/examine/getByEid?eid=' + id,
        crossOrigin: true,
        method: 'get',
        success: function(result) {
          callback(result)
        }
      })
    },
    handleFilter() {
      this.getListByID(this.handleSuccess)
    },
    handleModifyStatus(row, status, callback) {
      this.$message({
        message: '审批成功',
        type: 'success'
      })
      row.status = status
      if (row.status === '已通过') {
        reqwest({
          url: 'http://123.57.243.139:8080/examine/updateCabbage?eid=' + row.eid + '&status=2&type=3',
          crossOrigin: true,
          method: 'post',
          success: function(result) {
            callback(result)
          }
        })
      } else if (row.status === '未通过') {
        reqwest({
          url: 'http://123.57.243.139:8080/examine/updateCabbage?eid=' + row.eid + '&status=3&type=3',
          crossOrigin: true,
          method: 'post',
          success: function(result) {
            callback(result)
          }
        })
      } else {
        reqwest({
          url: 'http://123.57.243.139:8080/examine/updateCabbage?eid=' + row.eid + '&status=1&type=3',
          crossOrigin: true,
          method: 'post',
          success: function(result) {
            callback(result)
          }
        })
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row, index) {
      this.$notify({
        title: 'Success',
        message: '删除成功',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    }
  }
}
</script>
